package com.android.clinic.app.ui.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.clinic.app.ClinicApplication;
import com.android.clinic.app.R;
import com.android.clinic.app.adapter.BaseListAdapter;
import com.android.clinic.app.adapter.DoctorAdapter;
import com.android.clinic.app.base.BaseRefreshListActivity;
import com.android.clinic.app.entity.DoctorInfo;
import com.android.clinic.app.http.HttpClient;
import com.android.clinic.app.http.base.BaseObserver;
import com.android.clinic.app.http.utils.RxObservableUtils;
import com.android.clinic.app.ui.doctor.DoctorDetailActivity;
import com.kennyc.view.MultiStateView;

import java.util.List;

/**
 * Created by xiaming on 2018/3/24.
 * 关注的医生列表
 */

public class AttentionListActivity extends BaseRefreshListActivity<DoctorInfo> {

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_refresh_listview;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBarTitle("我的医生");
        setTitleBarLeftResource(R.drawable.icon_back);
        mAdapter.setOnItemClickListener((view, position) -> {
            Intent intent = new Intent(AttentionListActivity.this, DoctorDetailActivity.class);
            intent.putExtra("doctorId", mAdapter.getItem(position).getId());
            intent.putExtra("reservation", mAdapter.getItem(position).getReservation());
            intent.putExtra("evaluation", mAdapter.getItem(position).getEvaluation());
            startActivity(intent);
        });
    }

    @Override
    protected void reloadData() {
        HttpClient.getHttpService().getFollower(ClinicApplication.getUserInfo().getId())
                .compose(RxObservableUtils.applySchedulers())
                .subscribe(new BaseObserver<List<DoctorInfo>>() {
                    @Override
                    public void onSuccess(List<DoctorInfo> doctorInfoList) {
                        refreshComplete();
                        if (doctorInfoList.size() > 0) {
                            setMultiStateView(MultiStateView.VIEW_STATE_CONTENT);

                            int doctorSize = doctorInfoList.size();
                            for (int i = 0 ;i < doctorSize; i++) {
                                doctorInfoList.get(i).setReservation(String.valueOf((int)(Math.random()*1000)));
                                doctorInfoList.get(i).setEvaluation(String.valueOf((int)(Math.random()*100)));
                            }

                            for (DoctorInfo doctorInfo : doctorInfoList)
                                mAdapter.add(doctorInfo);
                        } else {
                            setMultiStateView(MultiStateView.VIEW_STATE_EMPTY);
                        }
                    }

                    @Override
                    public void onFailure(String message) {
                        setMultiStateView(MultiStateView.VIEW_STATE_EMPTY);
                        refreshComplete();
                        showToast(message);
                    }
                });
    }

    @Override
    protected BaseListAdapter<DoctorInfo> getAdapter() {
        return new DoctorAdapter(this);
    }
}
